package com.example.star.presenter;

import com.example.star.contract.UserContract;
import com.example.star.entity.User;

public class UserPresenter implements UserContract.Presenter {
    private UserContract.View view;
    private UserContract.Model model;

    public UserPresenter(UserContract.View view) {
        this.view = view;
        this.model = new com.example.star.model.UserModel();
    }

    @Override
    public void login(User user) {
        view.showLoading();
        model.login(user, new UserContract.Model.OnLoginListener() {
            @Override
            public void onSuccess() {
                view.hideLoading();
                view.onLoginSuccess();
            }

            @Override
            public void onFailure(String message) {
                view.hideLoading();
                view.onLoginFailure(message);
                view.showError(message); // 调用 showError 方法显示错误信息
            }
        });
    }

    @Override
    public void register(User user) {
        view.showLoading();
        model.register(user, new UserContract.Model.OnRegisterListener() {
            @Override
            public void onSuccess() {
                view.hideLoading();
                view.onRegisterSuccess();
            }

            @Override
            public void onFailure(String message) {
                view.hideLoading();
                view.onRegisterFailure(message);
                view.showPasswordMismatch();
                view.showError(message); // 调用 showError 方法显示错误信息

            }
        });
    }
}
